package logic

import (
	"context"
	"fmt"
	"jd-mall/jd-srv/model"

	"jd-mall/jd-srv/internal/svc"
	"jd-mall/jd-srv/jd"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShowAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShowAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShowAddLogic {
	return &ShowAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 购物车添加
func (l *ShowAddLogic) ShowAdd(in *jd.ShowAddRequest) (*jd.ShowAddResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	var shop model.Shop
	shop.ID = uint(in.ShopId)
	if shop.ShopList(); err != nil {
		logx.Errorf("商品不存在")
		return nil, fmt.Errorf("商品不存在")
	}
	if shop.Stock != int(in.Num) {
		logx.Errorf("库存不足")
		return nil, fmt.Errorf("库存不足")
	}
	shops := model.Shop{
		Name:  in.Name,
		Price: int(in.Price),
		Num:   int(in.Num),
		Stock: int(in.Stock),
		Img:   in.Img,
		State: int(in.State),
	}
	model.DB.Create(&shops)
	return &jd.ShowAddResponse{
		Id: int64(shops.ID),
	}, nil
}
